using System;
using System.Drawing;
using System.Windows.Forms;

namespace StarSkipGame
{
    /// <summary>
    /// 游戏设置窗体
    /// </summary>
    public partial class SettingsForm : Form
    {
        private GameSettings settings;
        private TabControl tabControl;
        private Button btnSave;
        private Button btnCancel;
        private Button btnReset;

        // 音效设置控件
        private CheckBox chkSoundEffects;
        private CheckBox chkBackgroundMusic;
        private TrackBar trkSoundVolume;
        private TrackBar trkMusicVolume;
        private Label lblSoundVolume;
        private Label lblMusicVolume;

        // 游戏设置控件
        private CheckBox chkShowFPS;
        private CheckBox chkShowParticles;
        private CheckBox chkShowTrails;
        private ComboBox cboDefaultGameMode;
        private Label lblDefaultGameMode;
        
        // 演示模式设置控件
        private CheckBox chkAutoDemoMode;
        private TrackBar trkDemoDuration;
        private Label lblDemoDuration;

        // 界面设置控件
        private CheckBox chkShowStatistics;
        private CheckBox chkShowAchievements;

        // 控制设置控件
        private CheckBox chkInvertControls;
        private TrackBar trkMouseSensitivity;
        private Label lblMouseSensitivity;

        // 性能设置控件
        private ComboBox cboFrameRate;
        private CheckBox chkVSync;
        private Label lblFrameRate;

        public SettingsForm()
        {
            settings = GameSettings.Instance;
            InitializeComponent();
            LoadSettings();
        }

        private void InitializeComponent()
        {
            this.Text = "游戏设置";
            this.Size = new Size(500, 600);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            // 创建主布局
            tabControl = new TabControl();
            tabControl.Dock = DockStyle.Top;
            tabControl.Size = new Size(480, 480);

            // 创建选项卡
            CreateAudioTab();
            CreateGameTab();
            CreateInterfaceTab();
            CreateControlTab();
            CreatePerformanceTab();

            // 创建按钮
            CreateButtons();

            // 添加控件到窗体
            this.Controls.Add(tabControl);
            this.Controls.Add(btnSave);
            this.Controls.Add(btnCancel);
            this.Controls.Add(btnReset);

            // 设置按钮位置
            btnSave.Location = new Point(300, 500);
            btnCancel.Location = new Point(380, 500);
            btnReset.Location = new Point(20, 500);
        }

        private void CreateAudioTab()
        {
            TabPage audioTab = new TabPage("音效设置");
            audioTab.Padding = new Padding(10);

            // 音效开关
            chkSoundEffects = new CheckBox();
            chkSoundEffects.Text = "启用音效";
            chkSoundEffects.Location = new Point(20, 20);
            chkSoundEffects.Size = new Size(150, 20);

            // 背景音乐开关
            chkBackgroundMusic = new CheckBox();
            chkBackgroundMusic.Text = "启用背景音乐";
            chkBackgroundMusic.Location = new Point(20, 50);
            chkBackgroundMusic.Size = new Size(150, 20);

            // 音效音量
            lblSoundVolume = new Label();
            lblSoundVolume.Text = "音效音量: 100%";
            lblSoundVolume.Location = new Point(20, 90);
            lblSoundVolume.Size = new Size(150, 20);

            trkSoundVolume = new TrackBar();
            trkSoundVolume.Location = new Point(20, 120);
            trkSoundVolume.Size = new Size(200, 45);
            trkSoundVolume.Minimum = 0;
            trkSoundVolume.Maximum = 100;
            trkSoundVolume.TickFrequency = 10;
            trkSoundVolume.ValueChanged += (s, e) => 
            {
                lblSoundVolume.Text = string.Format("音效音量: {0}%", trkSoundVolume.Value);
            };

            // 背景音乐音量
            lblMusicVolume = new Label();
            lblMusicVolume.Text = "背景音乐音量: 70%";
            lblMusicVolume.Location = new Point(20, 180);
            lblMusicVolume.Size = new Size(150, 20);

            trkMusicVolume = new TrackBar();
            trkMusicVolume.Location = new Point(20, 210);
            trkMusicVolume.Size = new Size(200, 45);
            trkMusicVolume.Minimum = 0;
            trkMusicVolume.Maximum = 100;
            trkMusicVolume.TickFrequency = 10;
            trkMusicVolume.ValueChanged += (s, e) => 
            {
                lblMusicVolume.Text = string.Format("背景音乐音量: {0}%", trkMusicVolume.Value);
            };

            // 添加控件到选项卡
            audioTab.Controls.AddRange(new Control[] 
            {
                chkSoundEffects, chkBackgroundMusic,
                lblSoundVolume, trkSoundVolume,
                lblMusicVolume, trkMusicVolume
            });

            tabControl.TabPages.Add(audioTab);
        }

        private void CreateGameTab()
        {
            TabPage gameTab = new TabPage("游戏设置");
            gameTab.Padding = new Padding(10);

            // 显示FPS
            chkShowFPS = new CheckBox();
            chkShowFPS.Text = "显示帧率";
            chkShowFPS.Location = new Point(20, 20);
            chkShowFPS.Size = new Size(150, 20);

            // 显示粒子效果
            chkShowParticles = new CheckBox();
            chkShowParticles.Text = "显示粒子效果";
            chkShowParticles.Location = new Point(20, 50);
            chkShowParticles.Size = new Size(150, 20);

            // 显示尾迹
            chkShowTrails = new CheckBox();
            chkShowTrails.Text = "显示尾迹效果";
            chkShowTrails.Location = new Point(20, 80);
            chkShowTrails.Size = new Size(150, 20);

            // 默认游戏模式
            lblDefaultGameMode = new Label();
            lblDefaultGameMode.Text = "默认游戏模式:";
            lblDefaultGameMode.Location = new Point(20, 120);
            lblDefaultGameMode.Size = new Size(150, 20);

            cboDefaultGameMode = new ComboBox();
            cboDefaultGameMode.Location = new Point(20, 150);
            cboDefaultGameMode.Size = new Size(200, 25);
            cboDefaultGameMode.DropDownStyle = ComboBoxStyle.DropDownList;
            cboDefaultGameMode.Items.AddRange(new object[] 
            {
                "经典模式", "生存模式", "时间挑战模式", "无尽模式", "Boss挑战模式"
            });

            // 演示模式设置
            chkAutoDemoMode = new CheckBox();
            chkAutoDemoMode.Text = "启用自动演示模式";
            chkAutoDemoMode.Location = new Point(20, 190);
            chkAutoDemoMode.Size = new Size(200, 20);

            lblDemoDuration = new Label();
            lblDemoDuration.Text = "演示持续时间: 30秒";
            lblDemoDuration.Location = new Point(20, 220);
            lblDemoDuration.Size = new Size(200, 20);

            trkDemoDuration = new TrackBar();
            trkDemoDuration.Location = new Point(20, 250);
            trkDemoDuration.Size = new Size(200, 45);
            trkDemoDuration.Minimum = 10;
            trkDemoDuration.Maximum = 120;
            trkDemoDuration.TickFrequency = 10;
            trkDemoDuration.ValueChanged += (s, e) => 
            {
                lblDemoDuration.Text = $"演示持续时间: {trkDemoDuration.Value}秒";
            };

            // 添加控件到选项卡
            gameTab.Controls.AddRange(new Control[] 
            {
                chkShowFPS, chkShowParticles, chkShowTrails,
                lblDefaultGameMode, cboDefaultGameMode,
                chkAutoDemoMode, lblDemoDuration, trkDemoDuration
            });

            tabControl.TabPages.Add(gameTab);
        }

        private void CreateInterfaceTab()
        {
            TabPage interfaceTab = new TabPage("界面设置");
            interfaceTab.Padding = new Padding(10);

            // 显示游戏统计
            chkShowStatistics = new CheckBox();
            chkShowStatistics.Text = "显示游戏统计";
            chkShowStatistics.Location = new Point(20, 20);
            chkShowStatistics.Size = new Size(150, 20);

            // 显示成就通知
            chkShowAchievements = new CheckBox();
            chkShowAchievements.Text = "显示成就通知";
            chkShowAchievements.Location = new Point(20, 50);
            chkShowAchievements.Size = new Size(150, 20);

            // 添加控件到选项卡
            interfaceTab.Controls.AddRange(new Control[] 
            {
                chkShowStatistics, chkShowAchievements
            });

            tabControl.TabPages.Add(interfaceTab);
        }

        private void CreateControlTab()
        {
            TabPage controlTab = new TabPage("控制设置");
            controlTab.Padding = new Padding(10);

            // 反转控制
            chkInvertControls = new CheckBox();
            chkInvertControls.Text = "反转控制方向";
            chkInvertControls.Location = new Point(20, 20);
            chkInvertControls.Size = new Size(150, 20);

            // 鼠标灵敏度
            lblMouseSensitivity = new Label();
            lblMouseSensitivity.Text = "鼠标灵敏度: 1.0x";
            lblMouseSensitivity.Location = new Point(20, 60);
            lblMouseSensitivity.Size = new Size(150, 20);

            trkMouseSensitivity = new TrackBar();
            trkMouseSensitivity.Location = new Point(20, 90);
            trkMouseSensitivity.Size = new Size(200, 45);
            trkMouseSensitivity.Minimum = 10;
            trkMouseSensitivity.Maximum = 30;
            trkMouseSensitivity.TickFrequency = 2;
            trkMouseSensitivity.ValueChanged += (s, e) => 
            {
                float sensitivity = trkMouseSensitivity.Value / 10.0f;
                lblMouseSensitivity.Text = string.Format("鼠标灵敏度: {0:F1}x", sensitivity);
            };

            // 添加控件到选项卡
            controlTab.Controls.AddRange(new Control[] 
            {
                chkInvertControls, lblMouseSensitivity, trkMouseSensitivity
            });

            tabControl.TabPages.Add(controlTab);
        }

        private void CreatePerformanceTab()
        {
            TabPage performanceTab = new TabPage("性能设置");
            performanceTab.Padding = new Padding(10);

            // 目标帧率
            lblFrameRate = new Label();
            lblFrameRate.Text = "目标帧率:";
            lblFrameRate.Location = new Point(20, 20);
            lblFrameRate.Size = new Size(150, 20);

            cboFrameRate = new ComboBox();
            cboFrameRate.Location = new Point(20, 50);
            cboFrameRate.Size = new Size(200, 25);
            cboFrameRate.DropDownStyle = ComboBoxStyle.DropDownList;
            cboFrameRate.Items.AddRange(new object[] 
            {
                "30 FPS", "60 FPS", "120 FPS"
            });

            // 垂直同步
            chkVSync = new CheckBox();
            chkVSync.Text = "启用垂直同步";
            chkVSync.Location = new Point(20, 90);
            chkVSync.Size = new Size(150, 20);

            // 添加控件到选项卡
            performanceTab.Controls.AddRange(new Control[] 
            {
                lblFrameRate, cboFrameRate, chkVSync
            });

            tabControl.TabPages.Add(performanceTab);
        }

        private void CreateButtons()
        {
            btnSave = new Button();
            btnSave.Text = "保存";
            btnSave.Size = new Size(75, 30);
            btnSave.Click += BtnSave_Click;

            btnCancel = new Button();
            btnCancel.Text = "取消";
            btnCancel.Size = new Size(75, 30);
            btnCancel.Click += BtnCancel_Click;

            btnReset = new Button();
            btnReset.Text = "重置默认";
            btnReset.Size = new Size(100, 30);
            btnReset.Click += BtnReset_Click;
        }

        private void LoadSettings()
        {
            // 音效设置
            chkSoundEffects.Checked = settings.SoundEffectsEnabled;
            chkBackgroundMusic.Checked = settings.BackgroundMusicEnabled;
            trkSoundVolume.Value = (int)(settings.SoundEffectsVolume * 100);
            trkMusicVolume.Value = (int)(settings.BackgroundMusicVolume * 100);

            // 游戏设置
            chkShowFPS.Checked = settings.ShowFPS;
            chkShowParticles.Checked = settings.ShowParticleEffects;
            chkShowTrails.Checked = settings.ShowTrails;
            cboDefaultGameMode.SelectedIndex = settings.DefaultGameMode;
            
            // 演示模式设置
            chkAutoDemoMode.Checked = settings.AutoDemoMode;
            trkDemoDuration.Value = (int)settings.DemoModeDuration;

            // 界面设置
            chkShowStatistics.Checked = settings.ShowGameStatistics;
            chkShowAchievements.Checked = settings.ShowAchievementNotifications;

            // 控制设置
            chkInvertControls.Checked = settings.InvertControls;
            trkMouseSensitivity.Value = (int)(settings.MouseSensitivity * 10);

            // 性能设置
            switch (settings.TargetFrameRate)
            {
                case 30: cboFrameRate.SelectedIndex = 0; break;
                case 60: cboFrameRate.SelectedIndex = 1; break;
                case 120: cboFrameRate.SelectedIndex = 2; break;
                default: cboFrameRate.SelectedIndex = 1; break;
            }
            chkVSync.Checked = settings.EnableVSync;
        }

        private void SaveSettings()
        {
            // 音效设置
            settings.SoundEffectsEnabled = chkSoundEffects.Checked;
            settings.BackgroundMusicEnabled = chkBackgroundMusic.Checked;
            settings.SoundEffectsVolume = trkSoundVolume.Value / 100.0f;
            settings.BackgroundMusicVolume = trkMusicVolume.Value / 100.0f;

            // 游戏设置
            settings.ShowFPS = chkShowFPS.Checked;
            settings.ShowParticleEffects = chkShowParticles.Checked;
            settings.ShowTrails = chkShowTrails.Checked;
            settings.DefaultGameMode = cboDefaultGameMode.SelectedIndex;
            
            // 演示模式设置
            settings.AutoDemoMode = chkAutoDemoMode.Checked;
            settings.DemoModeDuration = trkDemoDuration.Value;

            // 界面设置
            settings.ShowGameStatistics = chkShowStatistics.Checked;
            settings.ShowAchievementNotifications = chkShowAchievements.Checked;

            // 控制设置
            settings.InvertControls = chkInvertControls.Checked;
            settings.MouseSensitivity = trkMouseSensitivity.Value / 10.0f;

            // 性能设置
            switch (cboFrameRate.SelectedIndex)
            {
                case 0: settings.TargetFrameRate = 30; break;
                case 1: settings.TargetFrameRate = 60; break;
                case 2: settings.TargetFrameRate = 120; break;
                default: settings.TargetFrameRate = 60; break;
            }
            settings.EnableVSync = chkVSync.Checked;

            // 保存到文件
            settings.SaveSettings();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            SaveSettings();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要重置所有设置为默认值吗？", "确认重置", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                settings.ResetToDefaults();
                LoadSettings();
            }
        }
    }
} 